package com.blockmeta.bbs.businesslibrary.widget.chartview.Layer;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;


import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.businesslibrary.util.Format2Util;
import com.blockmeta.bbs.businesslibrary.widget.chartview.Axis.Coordinate;
import com.blockmeta.bbs.businesslibrary.widget.chartview.CandleData;
import com.blockmeta.bbs.businesslibrary.widget.chartview.ChartData;

import java.util.HashMap;
import java.util.List;

class BarLayer extends BasicLayer
{
	public BarLayer()
	{
		super();
	}

	@Override
	public Layer.ChartTypeEnum getLayerTypeEnum()
	{
		return Layer.ChartTypeEnum.BAR;
	}

	@Override
	public void onDrawLayer(Canvas pCanvas, Paint pPaint, Point pPoint)
	{
		Coordinate tCoordinate = m_callback.getCoordinate();
		List<CandleData> tDataList = m_callback.getCandleDataList();

		RectF layer = tCoordinate.getLayerRectF();

		// Bar 线的上分界线
//		pPaint.setColor(0xFFD3D8DE);
//		pPaint.setStrokeWidth(DimentionsUtil.dp2px(1));
//		pCanvas.drawLine(layer.left, layer.top, layer.right, layer.top, pPaint);

		String tCloseText = null;
		String tOpenText = null;
		String tHighText = null;
		String tLowText = null;
		String tTimeText = null;
		String tValueText = null;
		float tVolumnText = 0;
		int tXPoint = pPoint.x;

		if (tDataList != null && tDataList.size() > 0)
		{
			int tListSize = tDataList.size();
			int tStartIndex = tCoordinate.getStartIndex();
			float tXFirstPosition = tCoordinate.getXFirstPosition();
			float tXInterval = tCoordinate.getXInterval();
			float tYFirstposition = tCoordinate.getLayerRectF().bottom;
			
			float tYMaxValue = tCoordinate.getMaxValue();
			float tYMinValue = 0;//tCoordinate.getMinValue();
			float tYValueDist = tYMaxValue - tYMinValue;
			float tYValueLength = tCoordinate.getLayerRectF().bottom - tCoordinate.getLayerRectF().top;

			float tCurrentYPosition = 0.f;
			float tTempYPosition = 0.f;
			float tCurrentXPosition = 0.f;
			float tTempXPosition = 0.f;
			pPaint.setAntiAlias(true);
			pPaint.setColor(m_callback.getColor());
			pPaint.setStrokeWidth(2f);

			float tBarWidth = Math.min(tXInterval / 3, 5);

			int tSysUpColor = m_callback.getSysUpColor();
			int tSysDownColor = m_callback.getSysDownColor();
			int tSysEvenColor = m_callback.getSysEvenColor();

			for (int i = tStartIndex; i < tListSize; i++)
			{
				float tVolumn = tDataList.get(i).m_lVolume;
				float tOpen = (float) tDataList.get(i).getDbOpen();
				float tClose = (float) tDataList.get(i).getDbClose();
				
				tTempXPosition = tCurrentXPosition;
				tCurrentXPosition = tXFirstPosition + tXInterval * (i - tStartIndex);
				tTempYPosition = tCurrentYPosition;

				tCurrentYPosition = tYFirstposition - ((tVolumn - tYMinValue) * tYValueLength / tYValueDist);
				float tOpenYPosition = tYFirstposition - ((tOpen - tYMinValue) * tYValueLength / tYValueDist);
				float tCloseYPosition = tYFirstposition - ((tClose - tYMinValue) * tYValueLength / tYValueDist);

				if (tTempXPosition <= pPoint.x && pPoint.x < tCurrentXPosition)
				{
					float tValueaa = (tYFirstposition - pPoint.y) * tYValueDist / tYValueLength + tYMinValue;
					tValueText = FormatUtil.getFormatString(tValueaa, m_callback.getDigit());

					ChartData.ChartPeroid tChartType = m_callback.getChartType();

					if (i == tStartIndex)
					{
						tVolumnText = tDataList.get(i).m_lVolume;
						tCloseText = String.valueOf((float) tDataList.get(i).getDbClose());
						tOpenText = String.valueOf((float) tDataList.get(i).getDbOpen());
						tHighText = String.valueOf((float) tDataList.get(i).getDbHigh());
						tLowText = String.valueOf((float) tDataList.get(i).getDbLow());
						tTimeText = getDisplayX(tChartType, tDataList.get(i));
						tXPoint = (int) tCurrentXPosition;
					}
					else
					{
						tVolumnText = tDataList.get(i - 1).m_lVolume;
						tCloseText = String.valueOf((float) tDataList.get(i - 1).getDbClose());
						tOpenText = String.valueOf((float) tDataList.get(i - 1).getDbOpen());
						tHighText = String.valueOf((float) tDataList.get(i - 1).getDbHigh());
						tLowText = String.valueOf((float) tDataList.get(i - 1).getDbLow());
						tTimeText = getDisplayX(tChartType, tDataList.get(i - 1));
						tXPoint = (int) tTempXPosition;
					}
				}
				
				if (tOpen > tClose)
				{
					pPaint.setColor(tSysDownColor);
				}
				else if (tOpen == tClose)
				{
					pPaint.setColor(tSysEvenColor);
				}
				else
				{
					pPaint.setColor(tSysUpColor);
				}

				pCanvas.drawRect(tCurrentXPosition - tBarWidth, tCurrentYPosition, tCurrentXPosition + tBarWidth, tYFirstposition, pPaint);
			}
		}

		RectF tLayerRect = tCoordinate.getLayerRectF();
		setCrossData(pCanvas, pPaint, tTimeText, tValueText, new Point(tXPoint, pPoint.y));
		if (tLayerRect.contains(tXPoint, pPoint.y))
		{


			if (m_callback != null)
			{
				tCloseText = tCloseText == null ? "" : tCloseText;
				tOpenText = tOpenText == null ? "" : tOpenText;
				tHighText = tHighText == null ? "" : tHighText;
				tLowText = tLowText == null ? "" : tLowText;
				HashMap<String, String> tMap = new HashMap<String, String>();
				tMap.put("Close", tCloseText);
				tMap.put("Open", tOpenText);
				tMap.put("High", tHighText);
				tMap.put("Low", tLowText);
				tMap.put("Volumn", tVolumnText == 0 ? "" : Format2Util.doubleToHumanReadableFormat(tVolumnText, 2));
				m_callback.pointFeedback(tMap,pPoint );
			}
		}
	}

	@Override
	public void drawYesterClose(Canvas pCanvas, Paint pPaint)
	{
	}

}
